package mysql

import "go.uber.org/zap"

type commentDAO struct{}

var DefaultComment = commentDAO{}

// UpdateExZan 更新评论表字段 称赞的数量
func (c commentDAO) UpdateExZan(sn uint64, count int) error {
	sqlStr := `UPDATE st_comment SET zan_num = ? WHERE comment_sn = ?`
	_, err := db.Exec(sqlStr, count, sn)
	if err != nil {
		zap.L().Error("update comment zan num failed", zap.Error(err))
		return err
	}
	return err
}

// 根据评论编号返回作者编号
func (c commentDAO) GetAuthor(sn uint64) (author uint64, err error) {
	sqlStr := `SELECT user_sn FROM st_comment WHERE comment_sn = ? LIMIT 1`
	err = db.Get(&author, sqlStr, sn)
	if err != nil {
		zap.L().Error("according comment sn find user sn failed", zap.Error(err))
		return
	}
	return
}

// GetAuthorZan 返回该用户所有评论赞的总数
func (c commentDAO) GetAuthorZan(sn uint64) (count int, err error) {
	sqlStr := `SELECT sum(zan_num) FROM st_comment WHERE user_sn=?`
	err = db.Get(&count, sqlStr, sn)
	if err != nil {
		zap.L().Error("according user sn find zan count failed", zap.Error(err))
		return
	}
	return
}
